package org.mzy.org.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.mzy.basic.config.BussinessException;
import org.mzy.basic.service.impl.BaseServiceImpl;
import org.mzy.basic.util.BaiduAuditUtils;
import org.mzy.basic.util.EmailUtil;
import org.mzy.org.domain.Employee;
import org.mzy.org.domain.Shop;
import org.mzy.org.domain.ShopAuditLog;
import org.mzy.org.mapper.EmployeeMapper;
import org.mzy.org.mapper.ShopAuditLogMapper;
import org.mzy.org.mapper.ShopMapper;
import org.mzy.org.service.IShopService;
import org.mzy.org.vo.EchartVo;
import org.mzy.org.vo.ShopVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzy
 * @since 2022-10-15
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender sender;

    @Override
    public void settlement(Shop shop) {
        // 获取管理员名
        Employee admin = shop.getAdmin();
        // 调用自己定义的verify，传入shop和admin
        Employee employee = this.verify(shop, admin);
        // 调用添加方法，传入shop，添加店铺数据
        shopMapper.add(shop);
        // 如果employee为空
        if (null == employee) {
            // 将admin赋值给employee
            employee = admin;
            // 调用添加方法，传入employee，添加员工数据
            employeeMapper.add(employee);
        }
        // 修改员工数据管理店铺的id，赋值为店铺的id
        employee.setShopId(shop.getId());
        // 调用修改方法，传入employee
        employeeMapper.update(employee);
        // 声明一个shop对象
        Shop uptShop = new Shop();
        // 将店铺的id赋值为新对象的id
        uptShop.setId(shop.getId());
        // 将员工的id赋值为新对象的管理员id
        uptShop.setAdminId(employee.getId());
        // 调用修改方法，传入uptShop
        shopMapper.update(uptShop);
    }

    /*二次审核*/
    @Override
    public void again(ShopVo shopVo) {
        System.out.println(shopVo.getId());
        // 声明一个Shop对象
        Shop shop = new Shop();
        // 将shopVo中的数据拷贝到shop
        BeanUtils.copyProperties(shopVo, shop);
        System.out.println(shop.getId());
      /*  Shop shop1 = shopMapper.findByName(shop.getName());
        shop.setId(shop1.getId());*/
        // 给状态赋值为未激活
        shop.setState(0);
        // 调用修改方法
        shopMapper.update(shop);
    }

    @Override
    public void active(Long id) {
        Shop shop = new Shop();
        shop.setId(id);
        shop.setState(0);
        shopMapper.update(shop);
    }

    @Override
    public Map<String, Object> loadShopEchartsData() {
        // 获取表数据
        List<EchartVo> vo = shopMapper.loadShopEchartsData();
        // 将状态数据转换至list集合中
        List<Integer> x = vo.stream().map(EchartVo::getState).collect(Collectors.toList());
        // 将该状态的数量转换至list集合中
        List<Integer> y = vo.stream().map(EchartVo::getCount).collect(Collectors.toList());
        // 声明一个map集合
        Map<String, Object> data = new HashMap<>();
        // 将x和y轴的数据封装到map中返回
        data.put("x", x);
        data.put("y", y);
        return data;
    }

    @Override
    public void activite(Long id) {
        Shop shop = new Shop();
        shop.setId(id);
        shop.setState(2);
        shopMapper.update(shop);
    }

    @Override
    public void batchAdd(List<Shop> shops) {
        shopMapper.batchAdd(shops);
    }

    // 审核通过
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        // 需要去修改shop的状态为待激活
        Shop shop = new Shop();
        shop.setId(shopAuditLog.getShopId());
        shop.setState(1);
        shopMapper.update(shop);
        // 添加日志
        shopAuditLog.setAuditId(351L);
        shopAuditLog.setState(1);
        shopAuditLogMapper.add(shopAuditLog);
        // 发送邮件 邮件内容的连接是激活链接
        String text = "<h1>审核通过，请点击链接激活</h1> " +
                "<a href = 'http://localhost:8080/shop/activite/" + shopAuditLog.getShopId() + "'>点击激活</a>";
        EmailUtil.sendEmail("宠物乐园审核结果", text, "1071937734@qq.com");
    }

    // 审核驳回
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        // 修改shop的状态为 驳回
        Shop shop = new Shop();
        shop.setId(shopAuditLog.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);
        // 添加日志
//        shopAuditLog.setState(-1);
        shopAuditLog.setAuditId(351L);
        shopAuditLogMapper.add(shopAuditLog);
        // 发送邮件 邮件内容的链接是修改链接
        String text = "<h1>审核驳回，请点击链接修改审核内容</h1> " +
                "<a href = 'http://localhost:8081/#/registerAgain?shopId="+shopAuditLog.getShopId()+"'>点击修改店铺信息</a>";
        EmailUtil.sendEmail("宠物乐园审核结果", text, "1071937734@qq.com");
    }


    private Employee verify(Shop shop, Employee admin) {
        // 检测用户名是否为空
        if (StringUtils.isBlank(shop.getName())) {
            throw new BussinessException("店铺名不能为空");
        }
        // 判断店铺名称和logo是否合规
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BussinessException("店铺名称不合规");
        }
//        if (!BaiduAuditUtils.ImgCensor(shop.getLogo())) {
//            throw new BussinessException("店铺logo不合规");
//        }
        // 检测两次密码是否不一致
        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            throw new BussinessException("两次密码不一致");
        }
        // 获取店铺名
        Shop byName = shopMapper.findByName(shop.getName());
        // 检测店铺名是否存在
        if (null != byName) {
            throw new BussinessException("店铺名已重复");
        }
        // 根据名字获取管理员数据
        Employee byName1 = employeeMapper.findByName(admin.getUsername());
        // 如果管理员名不为空且管理员的管理店铺的id不为空
        if (null != byName1 && null != byName1.getShopId()) {
            // 根据所管理店铺的id获取管理员数据
            Shop byId = shopMapper.findById(byName1.getShopId());
            // 如果这个店铺的管理员的id和传入的管理员的数据id相同
            if (byId.getAdminId().equals(byName1.getId())) {
                throw new BussinessException("该管理员已经关联了其它店铺");
            }
        }
        return byName1;
    }
}
